<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, Label, Row, Col } from 'reactstrap';
import { AvForm, AvGroup, AvInput, AvField, AvFeedback } from 'availity-reactstrap-validation';
import { Translate, translate } from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

<%_ if (enableTranslation) { _%>
import { locales, languages } from 'app/config/translation';
<%_ } _%>
import { getUser, getRoles, updateUser, createUser, reset } from './user-management.reducer';
import { IRootState } from 'app/shared/reducers';

export interface IUserManagementUpdateProps extends StateProps, DispatchProps, RouteComponentProps<{ login: string }> {}

export const UserManagementUpdate = (props: IUserManagementUpdateProps) => {
  const [isNew] = useState(!props.match.params || !props.match.params.login);

  useEffect(() => {
    if (isNew) {
      props.reset();
    } else {
      props.getUser(props.match.params.login);
    }
    props.getRoles();
    return () => {
      props.reset();
    };
  }, []);

  const handleClose = () => {
    props.history.push('/admin/user-management');
  };

  const saveUser = (event, values) => {
    if (isNew) {
      props.createUser(values);
    } else {
      props.updateUser(values);
    }
    handleClose();
  };

  const isInvalid = false;
  const { user, loading, updating, roles } = props;

  return (
    <div>
      <Row className="justify-content-center">
        <Col md="8">
          <h1>
            <Translate contentKey="userManagement.home.createOrEditLabel">Create or edit a User</Translate>
          </h1>
        </Col>
      </Row>
      <Row className="justify-content-center">
        <Col md="8">
        { loading ? <p>Loading...</p>
        : <AvForm onValidSubmit={saveUser}>
          {user.id ? (
            <AvGroup>
              <Label for="id">
                <Translate contentKey="global.field.id">ID</Translate>
              </Label>
              <AvField type="text" className="form-control" name="id" required readOnly value={user.id} />
            </AvGroup>
          ) : null}
          <AvGroup>
            <Label for="login">
              <Translate contentKey="userManagement.login">Login</Translate>
            </Label>
            <AvField
              type="text"
              className="form-control"
              name="login"
              validate={{
                required: {
                  value: true,
                  errorMessage: translate('register.messages.validate.login.required')
                },
                pattern: {
                  value: "<%- LOGIN_REGEX %>",
                  errorMessage: translate('register.messages.validate.login.pattern')
                },
                minLength: {
                  value: 1,
                  errorMessage: translate('register.messages.validate.login.minlength')
                },
                maxLength: {
                  value: 50,
                  errorMessage: translate('register.messages.validate.login.maxlength')
                }
              }}
              value={user.login}
            />
          </AvGroup>
          <AvGroup>
            <Label for="firstName">
              <Translate contentKey="userManagement.firstName">First Name</Translate>
            </Label>
            <AvField
              type="text"
              className="form-control"
              name="firstName"
              validate={{
                maxLength: {
                  value: 50,
                  errorMessage: translate('entity.validation.maxlength', { max: 50 })
                }
              }}
              value={user.firstName}
              />
          </AvGroup>
          <AvGroup>
            <Label for="lastName">
              <Translate contentKey="userManagement.lastName">Last Name</Translate>
            </Label>
            <AvField
              type="text"
              className="form-control"
              name="lastName"
              validate={{
                maxLength: {
                  value: 50,
                  errorMessage: translate('entity.validation.maxlength', { max: 50 })
                }
              }}
              value={user.lastName}
            />
            <AvFeedback>This field cannot be longer than 50 characters.</AvFeedback>
          </AvGroup>
          <AvGroup>
            <AvField
              name="email"
              label={translate('global.form.email.label')}
              placeholder={translate('global.form.email.placeholder')}
              type="email"
              validate={{
                required: {
                  value: true,
                  errorMessage: translate('global.messages.validate.email.required')
                },
                email: {
                  errorMessage: translate('global.messages.validate.email.invalid')
                },
                minLength: {
                  value: 5,
                  errorMessage: translate('global.messages.validate.email.minlength')
                },
                maxLength: {
                  value: 254,
                  errorMessage: translate('global.messages.validate.email.maxlength')
                }
            }}
            value={user.email}
          />
          </AvGroup>
          <AvGroup check>
            <Label>
              <AvInput type="checkbox" name="activated" value={user.activated} checked={user.activated} disabled={!user.id} /> <Translate contentKey="userManagement.activated">Activated</Translate>
            </Label>
          </AvGroup>
  <%_ if (enableTranslation) { _%>
          <AvGroup>
            <Label for="langKey">
              <Translate contentKey="userManagement.langKey">Language Key</Translate>
            </Label>
            <AvField type="select" className="form-control" name="langKey" value={user.langKey || locales[0]}>
              {locales.map(locale =>
                <option value={locale} key={locale}>
                  {languages[locale].name}
                </option>
              )}
            </AvField>
          </AvGroup>
  <%_ } _%>
          <AvGroup>
            <Label for="authorities">
              <Translate contentKey="userManagement.profiles">Profiles</Translate>
            </Label>
            <AvInput type="select" className="form-control" name="authorities" value={user.authorities} multiple>
              {roles.map(role => (
                <option value={role} key={role}>
                  {role}
                </option>
              ))}
            </AvInput>
          </AvGroup>
          <Button tag={Link} to="/admin/user-management" replace color="info">
            <FontAwesomeIcon icon="arrow-left" />&nbsp;
            <span className="d-none d-md-inline">
              <Translate contentKey="entity.action.back">Back</Translate>
            </span>
          </Button>
          &nbsp;
          <Button color="primary" type="submit" disabled={isInvalid || updating}>
            <FontAwesomeIcon icon="save" />&nbsp;
            <Translate contentKey="entity.action.save">Save</Translate>
          </Button>
        </AvForm>
        }
        </Col>
      </Row>
    </div>
  );
};

const mapStateToProps = (storeState: IRootState) => ({
  user: storeState.userManagement.user,
  roles: storeState.userManagement.authorities,
  loading: storeState.userManagement.loading,
  updating: storeState.userManagement.updating
});

const mapDispatchToProps = { getUser, getRoles, updateUser, createUser, reset };

type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;

export default connect(mapStateToProps, mapDispatchToProps)(UserManagementUpdate);
